package com.banquetmanagementsystem.service.impl;

import com.banquetmanagementsystem.common.Utils.TokenUtil;
import com.banquetmanagementsystem.common.Utils.TokenUtilProperties;
import com.banquetmanagementsystem.common.constant.MessageConstant;
import com.banquetmanagementsystem.common.constant.TokenClaimsConstant;
import com.banquetmanagementsystem.common.context.BaseContext;
import com.banquetmanagementsystem.common.exception.CaptchaException;
import com.banquetmanagementsystem.common.exception.LoginFailedException;
import com.banquetmanagementsystem.mapper.RefreshTokenMapper;
import com.banquetmanagementsystem.mapper.UserInfoMapper;
import com.banquetmanagementsystem.pojo.dto.UserInfoLoginDTO;
import com.banquetmanagementsystem.pojo.entity.RefreshToken;
import com.banquetmanagementsystem.pojo.entity.UserInfo;
import com.banquetmanagementsystem.service.LoginService;
import com.banquetmanagementsystem.service.NewTokenService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 86176
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Resource(name = "userInfoMapper")
    private UserInfoMapper userInfoMapper;
    @Resource(name = "refreshTokenMapper")
    private RefreshTokenMapper refreshTokenMapper;
    @Resource
    private NewTokenService newTokenService;

    @Override
    public Map<String, String> login(UserInfoLoginDTO userInfoLoginDTO, HttpServletRequest request) {
        //验证码
        HttpSession session = request.getSession();
        String storedCaptcha = (String) session.getAttribute("captcha");
        if (storedCaptcha == null) {
            throw new CaptchaException(MessageConstant.VERIFICATION_CODE_EXPIRED);
        }
        if (!storedCaptcha.equalsIgnoreCase(userInfoLoginDTO.getCaptcha())) {
            throw new CaptchaException(MessageConstant.ERROR_IN_VERIFICATION_CODE);
        }
        session.removeAttribute("captcha");

        //登录验证
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(userInfoLoginDTO.getUsername());
        userInfo.setPassword(userInfoLoginDTO.getPassword());
        UserInfo user = userInfoMapper.checkUser(userInfo);
        if (user == null) {
            //失败
            throw new LoginFailedException(MessageConstant.INCORRECT_USERNAME_OR_PASSWORD);
        }
        Boolean isWeeklyDurationEnabled = userInfoLoginDTO.getIsWeeklyDurationEnabled();
        Map<String, String> claims = new HashMap<>(2);
        claims.put(TokenClaimsConstant.USERNAME, user.getUsername());
        claims.put(TokenClaimsConstant.USER_ID,user.getId().toString());
        claims.put(TokenClaimsConstant.IS_WEEKLY_DURATION_ENABLED,isWeeklyDurationEnabled.toString());
        return newTokenService.newToken(claims,user.getId());
    }
    @Override
    public void logout(Integer userId) {
        System.out.println(userId);
        refreshTokenMapper.updateRefreshTokenStatus(userId);
    }
}
